package com.leaves.common.util;


import lombok.SneakyThrows;
import net.sourceforge.pinyin4j.PinyinHelper;
import net.sourceforge.pinyin4j.format.HanyuPinyinCaseType;
import net.sourceforge.pinyin4j.format.HanyuPinyinOutputFormat;
import net.sourceforge.pinyin4j.format.HanyuPinyinToneType;
import net.sourceforge.pinyin4j.format.exception.BadHanyuPinyinOutputFormatCombination;


/**
 * @Author: LEAVES
 * @Version 1.0
 * @Date: 2021年03月09日  17时06分06秒
 * @Description: 获取中文得拼音或首字母
 */
public class ChineseCharToEn {


//    //<!--汉语拼音依赖-->
//    <dependency>
//        <groupId>com.belerweb</groupId>
//        <artifactId>pinyin4j</artifactId>
//        <version>2.5.0</version>
//    </dependency>

    /**
     * 获取中文的拼音
     *
     * @param name
     * @return
     * @throws BadHanyuPinyinOutputFormatCombination
     */
    public static String testPinyin(String name) throws BadHanyuPinyinOutputFormatCombination {
        char[] charArray = name.toCharArray();
        StringBuilder pinyin = new StringBuilder();
        HanyuPinyinOutputFormat defaultFormat = new HanyuPinyinOutputFormat();
        // 设置大小写格式
        defaultFormat.setCaseType(HanyuPinyinCaseType.UPPERCASE);
        // 设置声调格式：
        defaultFormat.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
        for (int i = 0; i < charArray.length; i++) {
            // 匹配中文,非中文转换会转换成null
            if (Character.toString(charArray[i]).matches("[\\u4E00-\\u9FA5]+")) {
                String[] hanyuPinyinStringArray = PinyinHelper
                        .toHanyuPinyinStringArray(charArray[i], defaultFormat);
                String string = hanyuPinyinStringArray[0];
                pinyin.append(string + " ");
            } else {
                pinyin.append(charArray[i]);
            }
        }
        //System.err.println(pinyin);
        return String.valueOf(pinyin);
    }

    /**
     * 获取中文拼音的首字母
     *
     * @param name
     * @return
     * @throws BadHanyuPinyinOutputFormatCombination
     */
    public static String testPinyinInitial(String name) throws BadHanyuPinyinOutputFormatCombination {
        char[] charArray = name.toCharArray();
        StringBuilder pinyin = new StringBuilder();
        HanyuPinyinOutputFormat defaultFormat = new HanyuPinyinOutputFormat();
        // 设置大小写格式
        defaultFormat.setCaseType(HanyuPinyinCaseType.UPPERCASE);
        // 设置声调格式：
        defaultFormat.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
        for (int i = 0; i < charArray.length; i++) {
            // 匹配中文,非中文转换会转换成null
            if (Character.toString(charArray[i]).matches("[\\u4E00-\\u9FA5]+")) {
                String[] hanyuPinyinStringArray = PinyinHelper
                        .toHanyuPinyinStringArray(charArray[i], defaultFormat);
                if (hanyuPinyinStringArray != null) {
                    pinyin.append(hanyuPinyinStringArray[0].charAt(0));
                }
            }
        }
        //System.err.println(pinyin);
        return String.valueOf(pinyin);
    }

    @SneakyThrows
    public static void main(String[] args) {
        //名字快捷转首字母小写示例 可直接复制粘贴使用
//         查询sql select concat('"',real_name,'"',',') FROM t_user_info_new
//        String [] names = {"木开代斯托合提",
//                "吾格来木买合木提"};
//        List<String> strings = Arrays.asList(names);
//        for (String string : strings) {
//            String[] split = string.split("·");
//            if (split.length == 1) System.out.println(testPinyinInitial(split[0]).toLowerCase(Locale.ROOT));
//            else {
//                String result = testPinyinInitial(split[0]).toLowerCase(Locale.ROOT).concat("_").concat(testPinyinInitial(split[1]).toLowerCase(Locale.ROOT));
//                System.out.println(result);
//
//            }
//        }

        String name = "小叶子";
        String str = testPinyinInitial(name);
        System.out.println("str = " + str);
    }
}
